package org.dromara.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.json.JSONUtil;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import org.dromara.business.constants.RepeaterConstant;
import org.dromara.business.domain.Events;
import org.dromara.business.service.EventsService;
import org.dromara.common.core.domain.R;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.redis.utils.RedisUtils;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/events")
public class EventsControlelr {

    private final EventsService eventsService;

    /**
     * 查询产品列表
     */
    @SaCheckPermission("business:events:list")
    @GetMapping("/list")
    public TableDataInfo<Events> list(Events bo, PageQuery pageQuery) {
        return eventsService.pageEvents(bo, pageQuery);
    }

    @SaCheckPermission("business:events:publish")
    @GetMapping("/publish/{id}")
    @Log(title = "审核通过", businessType = BusinessType.GRANT)
    public R<Boolean> publish(@PathVariable("id") String id) {
        Long userId = LoginHelper.getUserId();
        Map<String, Object> json = Maps.newHashMap();
        json.put("id", id);
        json.put("userId", userId);
        RedisUtils.publish(RepeaterConstant.REPEATER_CHECKED, JSONUtil.toJsonStr(json));
        return R.ok(true);
    }
}
